package com.xinxing.city.recovery.utils;

import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.util.Log;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

/**
 * Created by YFB on 2017/1/11.
 */

public class SuUtil {
    private static Process process;

    /**
     * 结束进程,执行操作调用即可
     */
    public static void kill(String packageName) {
        initProcess();
        killProcess(packageName);
        close();
    }

    public static void startApp(Context context,String pkg,String cls){
        ComponentName componetName = new ComponentName(pkg,
                cls);
        Intent intent = new Intent();
        intent.setComponent(componetName);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        try {
            context.startActivity(intent);
        }catch (ActivityNotFoundException exception){
            Log.e("test","启动失败");
        }
    }
    public static void start(String packageName, String className, Context context) {

//            initProcess();
        if(hasApplication(context,packageName))
            startProcess(packageName, className, context);
//            close();
    }
    public static boolean hasApplication(Context context, String packageName){
        PackageManager packageManager = context.getPackageManager();

        //获取系统中安装的应用包的信息
        List<PackageInfo> listPackageInfo = packageManager.getInstalledPackages(0);
        for (int i = 0; i < listPackageInfo.size(); i++) {
            if(listPackageInfo.get(i).packageName.equalsIgnoreCase(packageName)){
                return true;
            }
        }
        return false;

    }
    private static void startProcess(String packageName, String className, Context context) {
        Intent i = new Intent();
        ComponentName cn = new ComponentName(packageName, className);
        i.setComponent(cn);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            context.startForegroundService(i);
        } else {
            context.startService(i);
        }




//        OutputStream out = process.getOutputStream();
//        String cmd = "am start -n " + packageName + "/"+packageName+"."+className+" \n";
//        try {
//            out.write(cmd.getBytes());
//            out.flush();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
    }

    /**
     * 初始化进程
     */
    private static void initProcess() {
        if (process == null)
            try {
                process = Runtime.getRuntime().exec("su");
            } catch (IOException e) {
                e.printStackTrace();
            }
    }

    /**
     * 结束进程
     */
    private static void killProcess(String packageName) {

        OutputStream out = process.getOutputStream();
        String cmd = "am force-stop " + packageName + " \n";
        try {
            out.write(cmd.getBytes());
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 关闭输出流
     */
    private static void close() {
        if (process != null)
            try {
                process.getOutputStream().close();
                process = null;
            } catch (IOException e) {
                e.printStackTrace();
            }
    }


    public static boolean isRoot() {
        boolean bool = false;
        try {
            if ((new File("/system/bin/su").exists()) || (new File("/system/xbin/su").exists())) {
                bool = true;
            } else {
                bool = false;
            }

        } catch (Exception e) {

        }
        return bool;
    }


}